package com.doordu.soa.service.comm.redis;

import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastJsonRedisSerializer implements RedisSerializer<Object> {
	static final byte[] EMPTY_ARRAY = new byte[0];
	
	@Override
	public byte[] serialize(Object source) throws SerializationException {
		
		if (source == null) {
			return EMPTY_ARRAY;
		}
		return JSON.toJSONBytes(source,SerializerFeature.WriteClassName);
	}

	
	@Override
	public Object deserialize(byte[] bytes) throws SerializationException {
		if(bytes != null){
			
			if(bytes.length == 1 && bytes[0] == 0){
				return null;
			}
			
			return JSON.parse(bytes);
		}
		return null;
	}

}
